package com.suoluo.zenadmin.service.account;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.suoluo.zenadmin.dao.account.AccountDAO;
import com.suoluo.zenadmin.vo.account.AccountVO;
import com.suoluo.zenadmin.vo.account.OpenAccountVO;
import com.suoluo.zenadmin.vo.share.AuthedVO;
import com.suoluo.zenadmin.vo.share.ShareVO;

public class AccountServiceImpl implements AccountService{

	@Autowired
	private AccountDAO accountDAO;
	@Override
	public ShareVO findShareData(int shareType, String oriurl, int dataId) {
		return null;
	}

	
	@Override
	public AccountVO updateAuthed(AccountVO account, AuthedVO authedVO) {

		// 需要验证是否是绑定账号
		boolean bind = true;
		if (account == null) {
			bind = false;
			// 用户授权直接登录
			account = findAccount(authedVO.getOpenType(), authedVO.getOpenId());
		}
		
		// 老用户,直接登录
		if (account != null) {
			// 用户在已登录的情况下，绑定账号
			if (bind) {
				// 1.查询被绑定的账号是否已经受过权
				OpenAccountVO openAccount = accountDAO.findOpenAccount(authedVO.getOpenType(), authedVO.getOpenId());
				if (openAccount != null) {
					openAccount.setTokenExpire(authedVO.getExpireDate());
					openAccount.setToken(authedVO.getToken());
					openAccount.setScreenName(authedVO.getScreenName());
					int oldAccountId = openAccount.getAccountId();
					// 关联新的账号
					openAccount.setAccountId(account.getAccountId());
					// 修改被绑定的账号的属性，已经将被绑定的账号进行绑定
					accountDAO.updateOpenAccount(openAccount);
					// 删除被绑定的账号先前系统生成账号信息
					// 如果当前的账号与以前授权的账号不一致，则需要删除以前的绑定的系统账号
					if (oldAccountId != account.getAccountId()){
						accountDAO.deleteAccount(oldAccountId);
					}
				} else {
					// 没有授权的用户绑定账号
					openAccount = new OpenAccountVO();
					openAccount.setScreenName(authedVO.getScreenName());
					openAccount.setToken(authedVO.getToken());

					// 计算token有效时间
					openAccount.setTokenExpire(authedVO.getExpireDate());
					openAccount.setOpenId(authedVO.getOpenId());
					openAccount.setOpenType(authedVO.getOpenType());
					openAccount.setDomainName(authedVO.getDomainName());
					openAccount.setAccountId(account.getAccountId());
					// 保存新的授权用户
					int openAccountId = accountDAO.saveOpenAccount(openAccount);
					openAccount.setOpenAccountId(openAccountId);
				}
				// 将绑定的新账号加入到新的绑定中,每种平台类型只能绑定一个账号
				OpenAccountVO alreadyBindOpenAccount = account.getOpenAccount(authedVO.getOpenType());
				if (alreadyBindOpenAccount != null
						&& !alreadyBindOpenAccount.getOpenId().equals(
								openAccount.getOpenId())) {
					// 需要将原来的账号删除
					// logger.warn("repeat bind qq,delete old qq,name="+alreadyBindOpenAccount.getScreenName());
					account.removeOpenAccount(alreadyBindOpenAccount);
					accountDAO.deleteOpenAccount(alreadyBindOpenAccount.getOpenAccountId());
				}
				// 将绑定的新账号加入到新的绑定中
				account.addOpenAccount(openAccount);
			} else {
				// 用户在没有登录的情况下，登录
				// 更新用户的token有效期
				OpenAccountVO openAccount = account.getOpenAccount(authedVO
						.getOpenType());
				openAccount.setTokenExpire(authedVO.getExpireDate());
				openAccount.setToken(authedVO.getToken());
				openAccount.setScreenName(authedVO.getScreenName());
				// 更新Token的有效期
				accountDAO.updateOpenAccount(openAccount);
				account.setAccountName(authedVO.getScreenName());
				accountDAO.updateAccount(account);
			}
		} else {// 新用户

			account = new AccountVO();
			account.setAccountName(authedVO.getScreenName());
			account.setAccountHead(authedVO.getHead());
			account.setAccountGender(authedVO.getGender());
			account.setAccountCreateTime(new Date());

			// 保存到本地的第三方表
			OpenAccountVO openAccount = new OpenAccountVO();
			openAccount.setScreenName(authedVO.getScreenName());
			openAccount.setToken(authedVO.getToken());
			// 计算token有效时间
			openAccount.setTokenExpire(authedVO.getExpireDate());
			openAccount.setOpenId(authedVO.getOpenId());
			openAccount.setOpenType(authedVO.getOpenType());
			openAccount.setDomainName(authedVO.getDomainName());
			// 调用方法保存用户信息接口
			int accountId = accountDAO.saveAccount(account);
			account.setAccountId(accountId);
			openAccount.setAccountId(accountId);
			// 保存第三方账号
			int openAccountId = accountDAO.saveOpenAccount(openAccount);
			openAccount.setOpenAccountId(openAccountId);
			//将第三方账号绑定到禅境旅行的账号上
			account.addOpenAccount(openAccount);

		}
		return account;

	}

	@Override
	public AccountVO findAccount(int openType, String openId) {
		OpenAccountVO openAccount = accountDAO.findOpenAccount(openType, openId);
		if (openAccount == null){
			return null;
		}
		AccountVO account = accountDAO.findAccount(openAccount.getAccountId());
		account.addOpenAccount(openAccount);
		return account;
	}


	@Override
	public AccountVO findAccount(int accountId) {
		AccountVO account = accountDAO.findAccount(accountId);
		if (account == null){
			return null;
		}
		List<OpenAccountVO> openAccounts = accountDAO.findOpenAccount(accountId);
		for (OpenAccountVO oa : openAccounts){
			account.addOpenAccount(oa);
		}
		return account;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public OpenAccountVO findOpenAccountByName(String userScreen) {
		return accountDAO.findOpenAccountByName(userScreen);
	}
}
